package org.ucas.faker.web.dao.patents;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import org.ucas.faker.web.entity.patents.Agency;

import java.util.List;

@Repository
public interface AgencyDao extends JpaRepository<Agency,Long> {

  @Query("SELECT a FROM Agency a WHERE a.id in (SELECT pa.id FROM PatentAgency pa WHERE pa.patentId = ?1)")
  public List<Agency> loadByPatentId(long id);

  @Query("SELECT a FROM Agency a WHERE a.kind = 0 AND a.id in (SELECT pa.id FROM PatentAgency pa WHERE pa.patentId = ?1)")
  public List<Agency> loadApplicants(long id);

  @Query("SELECT a FROM Agency a WHERE a.kind = 1 AND a.id in (SELECT pa.id FROM PatentAgency pa WHERE pa.patentId = ?1)")
  public List<Agency> loadInventors(long id);

  public Agency findByWxIdAndKind(String wxId, Agency.Category kind);
  
  public List<Agency> findByKind(Agency.Category kind);

  @Modifying
  @Query("DELETE FROM PatentAgency pa WHERE pa.patentId = ?1")
  public void deleteByPatentId(long patentId);
  
  
  @Query("SELECT a from Agency a WHERE a.id in (?1) and a.kind = 1")
  public List<Agency> findById(List<Long> ids); 
}
